//
//  ArtInfoController.m
//  Arts1
//
//  Created by apple on 15/10/7.
//  Copyright © 2015年 Arts1. All rights reserved.
//

#import "ArtInfoController.h"
#import "AllHeader.h"
#import "ArtInfoTableViewCell.h"

//添加/取消收藏图片
//#define kMypictureURL @"http://arts1.dlab.com.cn/app/mypicture"
#define kMypictureURL @"http://arts1.cn/app/mypicture"

//删除图片
//#define kDelphotoURL @"http://arts1.dlab.com.cn/app/delphoto"
#define kDelphotoURL @"http://arts1.cn/app/delphoto"


@interface ArtInfoController ()

//作品详情页数据源
@property (nonatomic ,strong) NSMutableArray *dataSource;

@property (nonatomic ,assign) BOOL isEditing; //判断是否出去编辑状态

@end

@implementation ArtInfoController

#pragma mark - 懒加载
- (NSMutableArray *)dataSource
{
    if (_dataSource == nil)
    {
        self.dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.backgroundColor = kBackgroundColor;
    self.navigationItem.title = @"我的作品";
    //注册类
    [self.tableView registerClass:[ArtInfoTableViewCell class] forCellReuseIdentifier:@"cell"];
    //去掉分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self requestData];
    
    UIBarButtonItem *releaseButon=[[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(editingInfo:)];
    self.navigationItem.rightBarButtonItem=releaseButon;
}


//第一个分区请求数据
- (void)requestData
{
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //获取uid和key
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataForKeyAndUid"];
    
    NSString *uid = dic[@"data"][@"uid"];
    NSString *key = dic[@"data"][@"key"];
    NSDictionary *pardic = [NSDictionary dictionary];
    if (uid && key){
        pardic = @{@"uid":uid,@"key":key};
    }
    [manage POST:kMypictureURL parameters:pardic
         success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         if (responseObject)
         {
             NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
             int status = [dic[@"status"] intValue];
             
             if (status == 1)
             {
                 NSArray *list = dic[@"list"];
                 
                 for (NSDictionary *dict in list)
                 {
                     MypictureModel *model = [[MypictureModel alloc] init];
                     [model setValuesForKeysWithDictionary:dict];
                     [self.dataSource addObject:model];
                 }
             }
         }
         //刷新数据
         [self.tableView reloadData];
         // 请求成功的时候调用这个block
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         // 请求失败的时候调用调用这个block
         NSLog(@"请求失败");
     }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 *  点击编辑按钮
 *
 *  @param editing <#editing description#>
 */
- (void)editingInfo:(UIBarButtonItem *)editing
{
    self.isEditing = !self.isEditing;
    if (self.isEditing) {
        editing.title = @"完成";
    }
    else
    {
        editing.title = @"编辑";
    }
    [self.tableView reloadData];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ArtInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    MypictureModel *model = _dataSource[indexPath.row];
    cell.model = model;
    cell.backgroundColor = kBackgroundColor;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.isEditing)
    {
        [cell beginEditing];
    }
    else
    {
        [cell endEditing];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (230);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.isEditing) {
        //删除某个Cell
        MypictureModel *model = _dataSource[indexPath.row];
        //调用删除图片方法
        [self requestDelPhoto:model];
        [self.dataSource removeObjectAtIndex:indexPath.row];
        [self.tableView reloadData];
    }
}

//根据请求删除数据
- (void)requestDelPhoto:(MypictureModel *)model
{
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //获取uid和key
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataForKeyAndUid"];
    
    NSString *uid = dic[@"data"][@"uid"];
    NSString *key = dic[@"data"][@"key"];
    NSDictionary *pardic = [NSDictionary dictionary];
    
    
    if (uid && key && model.Id)
    {
        pardic = @{@"uid":uid,
                   @"key":key,
                   @"photo":model.Id};
    }
    
    [manage POST:kDelphotoURL parameters:pardic
         success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         if (responseObject)
         {
             NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
             int status = [dic[@"status"] intValue];
             
             if (status == 1)
             {
                 NSLog(@"删除成功");
             }
         }
         //刷新数据
         [self.tableView reloadData];
         // 请求成功的时候调用这个block
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         // 请求失败的时候调用调用这个block
         NSLog(@"请求失败");
     }];
}

@end
